//
//  VideoViewControllerViewController.m
//  jinneng
//
//  Created by mac on 2020/7/31.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "VideoViewControllerViewController.h"
#import "VideoClassViewController.h"
#import "VideoClass1ViewController.h"
#import "VideoClass2ViewController.h"
#import "GoodArticalViewController.h"
#import "LoginViewController.h"
#import "PointViewControllerViewController.h"
#import "ArtistViewController.h"
#import "ScanCodeViewController.h"

#import <NinaPagerView.h>
#import <UIParameter.h>
#import "UserCenterViewController.h"
#import "SearchViewController.h"
@interface VideoViewControllerViewController ()
@property(nonatomic,strong) JznWebView *jznWebView;
@property(nonatomic) int page;
@property(nonatomic,strong) NSString *module;
@property(nonatomic,strong) NSString * trade;
@property(nonatomic) CGRect frame;

@property(nonatomic,strong) JznWebView *titleView;
@property(nonatomic, strong) WKWebViewJavascriptBridge *titleBridge;

@property(nonatomic, strong) NinaPagerView *pageView;
@property(nonatomic, strong) NSMutableArray *titleArray;
@property(nonatomic, strong) NSMutableArray *vcsArray;
@end

@implementation VideoViewControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _module = @"[6,12]";
    _trade = @"";
    _frame = self.tabBarController.tabBar.frame;

    [self initView];
    [self registerNativeFunctions];
}

//-(void) viewWillAppear:(BOOL)animated{
//  // self.navigationController.navigationBar.hidden =YES;
//}

-(void) viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden =YES;
    self.tabBarController.tabBar.hidden = NO;
    self.tabBarController.tabBar.frame = _frame;
    [self requestPoint];
}

-(void) initView{
    CGRect statusFrame = [[UIApplication sharedApplication] statusBarFrame];
    self.titleView  = [[JznWebView alloc]initWithFrame:_frame];
    [self.view addSubview:self.titleView];
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.titleHeight+statusFrame.size.height);
        make.width.left.equalTo(self.view);
        make.top.equalTo(self.view).offset(-(5+statusFrame.size.height));
    }];
    if(statusFrame.size.height<44){
        [self.titleView loadLocalFile:@"/ios_jinneng/jinnengTitle_new_small" type:@"html"];
    }else{
        [self.titleView loadLocalFile:@"/ios_jinneng/jinnengTitle_new" type:@"html"];
    }
    
    self.titleBridge = [WKWebViewJavascriptBridge bridgeForWebView:self.titleView];
    [self.titleBridge setWebViewDelegate:self];
    
    _titleArray = [[NSMutableArray alloc]init];
    [_titleArray addObject:@"专业课堂"];
    //[_titleArray addObject:@"安全课堂"];
    [_titleArray addObject:@"知识课堂"];
    [_titleArray addObject:@"精选好文"];
    [_titleArray addObject:@"能工巧匠"];
    _vcsArray =[[NSMutableArray alloc]init];
    VideoClassViewController *vc1 = [[VideoClassViewController alloc]init];
    VideoClass1ViewController *vc2 = [[VideoClass1ViewController alloc]init];
    GoodArticalViewController *vc3 = [[GoodArticalViewController alloc]init];
    VideoClass2ViewController *vc4 = [[VideoClass2ViewController alloc]init];
    ArtistViewController *vc5 = [[ArtistViewController alloc]init];
    
    [_vcsArray addObject:vc1];
    //[_vcsArray addObject:vc4];
    [_vcsArray addObject:vc2];
    [_vcsArray addObject:vc3];
    [_vcsArray addObject:vc5];
    [self.view addSubview:self.pageView];
}

-(void) toLogin{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    if ([userDefault objectForKey:@"account"] == nil) {
        LoginViewController *controller = [[LoginViewController alloc]init];
        controller.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentModalViewController:controller animated:YES ];
        return;
    }
}

- (void)registerNativeFunctions {

    
    [self.titleBridge registerHandler:@"toPoint" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        dispatch_async(dispatch_get_main_queue(), ^(){

            PointViewControllerViewController *vc = [[PointViewControllerViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        });
        
    }];
    
    [self.titleBridge registerHandler:@"toUserCenter" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        dispatch_async(dispatch_get_main_queue(), ^(){

            UserCenterViewController *vc = [[UserCenterViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        });
        
    }];
    
    [self.titleBridge registerHandler:@"toSearch" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        dispatch_async(dispatch_get_main_queue(), ^(){

            SearchViewController *vc = [[SearchViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        });
        
    }];
    
    [self.titleBridge registerHandler:@"toScan" handler:^(id data, WVJBResponseCallback responseCallback) {
          
          dispatch_async(dispatch_get_main_queue(), ^(){
              NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
              if ([userDefault objectForKey:@"account"] == nil) {
                  LoginViewController *controller = [[LoginViewController alloc]init];
                  controller.tabIndex = 1;
                  controller.modalPresentationStyle = UIModalPresentationFullScreen;
                  [self presentModalViewController:controller animated:YES ];
                  return;
              }else{
                  ScanCodeViewController *learnNewsViewController = [[ScanCodeViewController alloc]init];
                  self.hidesBottomBarWhenPushed = YES;
                  [self.navigationController pushViewController:learnNewsViewController animated:YES];
              }
          });
          
      }];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
   
   [self requestPoint];
    
    NSString *js = [NSString stringWithFormat:@"getUserName('%@')",self.loginDto.userName];
    [self.titleView evaluateJavaScript:js completionHandler:nil];
    
    NSString *js1 = [NSString stringWithFormat:@"getCompanyName('%@')",self.loginDto.businessCompanyName];
    [self.titleView evaluateJavaScript:js1 completionHandler:nil];
}

-(void) requestPoint{
    NSString *url = [NSString stringWithFormat:@"%@/userRank/findUserScore",[URLUtil hostName]];
    NSDictionary *params = @{@"userId":[NSString stringWithFormat:@"%d",self.loginDto.userId]};
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            NSString *js = [NSString stringWithFormat:@"getPoint(%@)",dataResult.data];
            [self.titleView evaluateJavaScript:js completionHandler:nil];
        }else {
        }
    } failure:^(NSError *error) {
        
    }];
    NSString *url1 = [NSString stringWithFormat:@"%@/userEntry/userLoginCount",[URLUtil hostName]];
    NSDictionary *params1 = @{};
    [JZNRequestUtil requestUrl:url1 requestParams:params1 success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            NSString *js = [NSString stringWithFormat:@"getUserCount(%@)",dataResult.data];
            [self.titleView evaluateJavaScript:js completionHandler:nil];
        }else {
        }
    } failure:^(NSError *error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (NinaPagerView *)pageView {
    if (!_pageView) {
        /**
         *  创建ninaPagerView，控制器第一次是根据您划的位置进行相应的添加的，类似网易新闻虎扑看球等的效果，后面再滑动到相应位置时不再重新添加，如果想刷新数据，您可以在相应的控制器里加入刷新功能。需要注意的是，在创建您的控制器时，设置的frame为FUll_CONTENT_HEIGHT，即全屏高减去导航栏高度，如果这个高度不是您想要的，您可以去在下面的frame自定义设置。
         *  A tip you should know is that when init the VCs frames,the default frame i set is FUll_CONTENT_HEIGHT,it means fullscreen height - NavigationHeight - TabbarHeight.If the frame is not what you want,just set frame as you wish.
         */
        CGRect pagerRect = CGRectMake(0, self.titleHeight-12, FUll_VIEW_WIDTH,FUll_CONTENT_HEIGHT-TabbarHeight-30);
        _pageView = [[NinaPagerView alloc] initWithFrame:pagerRect WithTitles:self.titleArray WithObjects:self.vcsArray];
        _pageView.ninaPagerStyles = NinaPagerStyleBottomLine;
        _pageView.titleFont = 10*LSCALE;
        _pageView.topTabHeight = 25*LSCALE;
    }
    return _pageView;
}

- (void)tabBarController:(UITabBarController*) tabBarController didSelectViewController:(UIViewController*)viewController{
    if (self.tabBarController.tabBar.isHidden) {
        self.tabBarController.tabBar.hidden = NO;
      //  self.tabBarController.tabBar.frame = _frame;
    }
}
@end
